import {defineStore} from 'pinia'
import router from '@/router'
import {getToken, setToken, removeToken} from '@/utils/token'
import api from "@/api"
import http from "@/utils/http"
import { Local } from '@/utils/storage';

const userPiniaStore = defineStore('user', {
    state: () => ({
        userInfo:{
            avatar:'https://img2.baidu.com/it/u=1978192862,2048448374&fm=253&fmt=auto&app=138&f=JPEG?w=504&h=500',
            username:'王xb',
            roles:['admin'],
            permissions: ['home','components','previewImage','error','401','404'],
        },
    }),
    actions: {
        logout() {
            // return new Promise(async (resolve, reject) => {
            //     const {data: res} = await http.post(api.logout, {})
            //     if (res.code == 200) {
                    removeToken()
                    Local.remove('user')
                    router.push({path: '/login'})
            //         resolve()
            //     }
            // })
        }
    },
    getters: {
        getToken: (state) => {
            return state.token
        }
    },
    persist: {
        enabled: true,
        strategies: [
            {
                key: 'user',
                storage: localStorage,
                path: [] //可以选择保存的字段  其余的不保存
            }
        ]
    }
})

export default userPiniaStore
